<template>
  <div>
    <el-dialog
      ref="distributeDialog"
      :model-value="designOpen"
      :title="designTitle"
      width="500px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form
        ref="editForm"
        :model="form"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="开发人员类型:" prop="type" :label-position="labelPosition">
          <el-checkbox-group v-model="form.type">
            <el-checkbox label="前端"></el-checkbox>
            <el-checkbox label="后端"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="开发人员:" prop="operator" :label-position="labelPosition">
          <el-input
            v-model="form.operator"
            placeholder="请输入开发人员名称"
            type="textarea"
            clearable
            size="small"
          />
        </el-form-item>
        <el-form-item label="开发开始日期:" prop="startTime" :label-position="labelPosition">
          <el-date-picker
            v-model="form.startTime"
            align="right"
            type="date"
            placeholder="请选择开始日期"
            format="YYYY/MM/DD"
          />
        </el-form-item>
        <el-form-item label="开发结束日期:" prop="stopTime" :label-position="labelPosition">
          <el-date-picker
            v-model="form.stopTime"
            align="right"
            type="date"
            placeholder="请选择结束日期"
            format="YYYY/MM/DD"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div>
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="distributionSubmit">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import { ref, getCurrentInstance, defineEmits } from 'vue'
import { ElMessage } from 'element-plus'
import { updateSysDemandByOperator } from '@/api/demandManagement'

const props = defineProps({
  designTitle: {
    type: String,
  },
  designOpen: {
    type: Boolean,
  },
  parentId: {
    type: Number,
  },
})
const emit = defineEmits(['update:designOpen', 'changeList'])
const 
  labelPosition = ref('right'),
  rules = {
    type: [{ required: true, message: '请选择开发人员类型', trigger: ['blur', 'change'] }],
    startTime: [{ required: true, message: '请选择开始日期', trigger: ['blur', 'change'] }],
    stopTime: [{ required: true, message: '请选择结束日期', trigger: ['blur', 'change'] }],
  },
  form = ref({
    id: props.parentId,
    type: [],
    person: '',
    startTime: '',
    stopTime: '',
    operator: '',
  }),
  editForm = ref(null),
  {
    proxy,
  }:{
   proxy:any
 } = getCurrentInstance()
const reset = () => {
  form.value = {
    id: props.parentId,
    type: [],
    person: '',
    startTime: '',
    stopTime: '',
    operator: '',
  }
  proxy.$resetForm(editForm.value)
}
const cancel = () => {
  reset()
  emit('update:designOpen', false)
}
const distributionSubmit = () => {
  proxy.$validateForm(editForm.value, () => {
    updateSysDemandByOperator(form.value).then(res => {
      if (res.code == 200) {
        reset()
        emit('update:designOpen', false)
        ElMessage.success('修改成功')
        emit('changeList')
      }
    })
  })
}
</script>